﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;
using SmartSpace.Packaging;

namespace PackagingTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(App.FilePath))
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "all files(*.*)|*.*|theme def file.xtml(*.xtml)|*.xtml|module def file(*.xmml)|*.xmml";
                dlg.Multiselect = false;
                if (dlg.ShowDialog() == true)
                {
                    App.FilePath = dlg.FileName;
                }
                else
                {
                    Close();
                    return;
                }
            }

            FileInfo fi = new FileInfo(App.FilePath);
            if (fi.Extension == ".xtml")
            {
                PackageXTML();
            }
            else if (fi.Extension == ".xmml")
            {
                PackageXMML();
            }
            else
            {
                MessageBox.Show("文件格式不正确!");
            }
        }

        private void PackageXTML()
        {
            _ctlEditOutput.Text += string.Format("{0}{1}", "打包主题部署文件包中...", Environment.NewLine);

            XTemplate template = SmartSpace.Packaging.PackageUtil.ParseXTML(App.FilePath);
            FileInfo fi = new FileInfo(App.FilePath);
            string folder = fi.Directory.FullName;
            List<string> files = new List<string>();
            foreach (var xFile in template.Files)
            {
                files.Add(xFile.Path);
            }
            files.Add(fi.Name);

            foreach (var item in files)
            {
                _ctlEditOutput.Text += string.Format("需要打包文件:{0}{1}", item, Environment.NewLine);
            }

            string zipFilePath = string.Format("{0}\\{1}{2}.sstp", fi.Directory.FullName, fi.Name.Replace(fi.Extension, string.Empty), template.Version);

            SmartSpace.Packaging.PackageUtil.CompressFiles(folder, files, zipFilePath);

            _ctlEditOutput.Text += string.Format("{0}{1}", "打包完毕!", Environment.NewLine);
            _ctlEditOutput.Text += string.Format("文件路径:{0}{1}", zipFilePath, Environment.NewLine);
        }

        private void PackageXMML()
        {
            _ctlEditOutput.Text += string.Format("{0}{1}", "打包模块部署文件包中...", Environment.NewLine);

            XModule module = SmartSpace.Packaging.PackageUtil.ParseXMML(App.FilePath);
            FileInfo fi = new FileInfo(App.FilePath);
            string folder = fi.Directory.FullName;
            List<string> files = new List<string>();
            foreach (var xFile in module.Files)
            {
                files.Add(xFile.Path);
            }
            files.Add(fi.Name);

            foreach (var item in files)
            {
                _ctlEditOutput.Text += string.Format("需要打包文件:{0}{1}", item, Environment.NewLine);
            }

            string zipFilePath = string.Format("{0}\\{1}{2}.ssmp", fi.Directory.FullName, fi.Name.Replace(fi.Extension, string.Empty), module.Version);

            SmartSpace.Packaging.PackageUtil.CompressFiles(folder, files, zipFilePath);

            _ctlEditOutput.Text += string.Format("{0}{1}", "打包完毕!", Environment.NewLine);
            _ctlEditOutput.Text += string.Format("文件路径:{0}{1}", zipFilePath, Environment.NewLine);
        }

        private void _ctlBtnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
